﻿@inject IPermissionService PermissionService

@* <MudNavMenu> *@
@*     <MudNavLink Href="/" Match="NavLinkMatch.All">Dashboard</MudNavLink> *@
@*     <MudNavGroup Title="系统管理" Expanded="true"> *@
@*         <MudNavLink Href="/users/index" Match="NavLinkMatch.Prefix">用户管理</MudNavLink> *@
@*         <MudNavLink Href="/roles/index" Match="NavLinkMatch.Prefix">角色管理</MudNavLink> *@
@*         <MudNavLink Href="/permissions/index" Match="NavLinkMatch.Prefix">权限管理</MudNavLink> *@
@*         <MudNavLink Href="/security/index" Match="NavLinkMatch.Prefix">角色授权</MudNavLink> *@
@*     </MudNavGroup> *@

@* @Icons.Material.Filled.Settings *@
@* </MudNavMenu> *@

<MudSidebar Bordered="false" Items="_menus" Rounded="true" Color="Color.Primary">
    <div class="app-brand demo">
        <a href="index.html" class="app-brand-link">
            <span class="app-brand-logo demo">
                <span>
                    <MudIcon Icon="@Icons.Custom.Brands.MudBlazor" Color="Color.Primary" Size="Size.Large"/>
                </span>
            </span>
            <span class="app-brand-text demo menu-text fw-bold ms-2">MudBlazor</span>
        </a>
    </div>
</MudSidebar>

@code {
    private IList<MenuInfo> _menus = new List<MenuInfo>();

    protected override async Task OnInitializedAsync()
    {
        var result = await PermissionService.QueryPermissionsByCurrentUser(1);

        if (result.Succeeded)
        {
            GetMenuChild(result.Data, _menus);
        }

        await base.OnInitializedAsync();
    }

    private void GetMenuChild(IList<PermissionTreeResponseDto>? permissions, IList<MenuInfo> menus)
    {
        if (permissions != null)
        {
            foreach (var permission in permissions)
            {
                var menuInfo = new MenuInfo()
                {
                    Name = permission?.Name,
                    Icon = permission?.Icon,
                    Url = permission?.Url
                };

                if (permission?.Children != null)
                {
                    menuInfo.Children = new List<MenuInfo>();
                    GetMenuChild(permission.Children.ToList(), menuInfo.Children);
                }

                menus.Add(menuInfo);
            }
        }
    }

}